package com.qyhl.party.party.study.subject.item;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.qyhl.party.R;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class PartyNewsLongItemDelegate implements ItemViewDelegate<NewsBean> {
    private Context context;

    public PartyNewsLongItemDelegate(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.party_item_news_long;
    }

    @Override
    public boolean isForViewType(NewsBean item, int position) {
        return item.getPluralPicsFlag() == 1 && item.getImagess() != null && item.getImagess().size() > 0;
    }

    @Override
    public void convert(ViewHolder holder, NewsBean newsBean, int position) {
        holder.setText(R.id.title, newsBean.getTitle());

        if (StringUtils.isNotEmpty(newsBean.getSource())) {
            holder.setVisible(R.id.origin, true);
            holder.setText(R.id.origin, newsBean.getSource());
        } else {
            holder.setVisible(R.id.origin, false);
        }

        try {
            int minPlay = Integer.parseInt(CommonUtils.getInstance().getMinPlay());
            if (minPlay == 0) {
                holder.setVisible(R.id.scan_num, false);
            } else {
                //判断评论数是否大于最少浏览数
                if (StringUtils.isEmpty(newsBean.getHitCount() + "")) {
                    holder.setVisible(R.id.scan_num, false);
                } else if (Integer.parseInt(newsBean.getHitCount()) >=
                        minPlay) {
                    holder.setText(R.id.scan_num, StringUtils.transformNum(newsBean.getHitCount()) + "阅");
                    holder.setVisible(R.id.scan_num, true);
                } else {
                    holder.setVisible(R.id.scan_num, false);
                }
            }

        } catch (Exception e) {
            holder.setVisible(R.id.scan_num, false);
        }

        //三图
        ImageView icon_one = holder.getView(R.id.cover_one);
        ImageView icon_two = holder.getView(R.id.cover_two);
        ImageView icon_three = holder.getView(R.id.cover_three);

        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.cover_large_default)
                .error(R.drawable.cover_large_default);


        List<NewsBean.Images> imageList = new ArrayList<>();
        if (newsBean.getImagess() != null) {
            holder.setVisible(R.id.image_layout, true);
            if (newsBean.getImagess().size() > 3) {
                for (int i = 0; i < 3; i++) {
                    imageList.add(newsBean.getImagess().get(i));
                }
            } else {
                imageList.addAll(newsBean.getImagess());
            }

            icon_one.setVisibility(View.INVISIBLE);
            icon_one.setVisibility(View.INVISIBLE);
            icon_three.setVisibility(View.INVISIBLE);

            for (int i = 0; i < imageList.size(); i++) {
                switch (i) {
                    case 0:
                        icon_one.setVisibility(View.VISIBLE);
                        Glide.with(context)
                                .load(newsBean.getImagess().get(i).getImageUrlString())
                                .apply(options)
                                .into(icon_one);

                        break;
                    case 1:
                        icon_two.setVisibility(View.VISIBLE);
                        Glide.with(context)
                                .load(newsBean.getImagess().get(i).getImageUrlString())
                                .apply(options)
                                .into(icon_two);
                        break;
                    case 2:
                        icon_three.setVisibility(View.VISIBLE);
                        Glide.with(context)
                                .load(newsBean.getImagess().get(i).getImageUrlString())
                                .apply(options)
                                .into(icon_three);
                        break;
                }
            }

        } else {
            holder.setVisible(R.id.image_layout, false);
        }

    }
}
